qtre.sceneNode = function(name){
	this.name = name;

	this.worldMatrix = new Float32Array(16);
	tdl.fast.matrix4.identity(this.worldMatrix);

	this.children = [];

	this.parent = null;

	this._objects = [];

	this._childrenNum = 0;
	
	this._onRenderBegins = [];
}

qtre.sceneNode.prototype = {

	addRenderBeginCallback : function(callback){
		this._onRenderBegins.push(callback);
	},

	removeRenderBeginCallback : function(callback){
		for(var i = 0;  i < this._onRenderBegins.length; i++){
			if(this._onRenderBegins[i] == callback){
				delete this._onRenderBegins[i];
			}
		}
	},
	
	onRenderBegin : function(){
		for(var i = 0;  i < this._onRenderBegins.length; i++){
			this._onRenderBegins[i].call(this);
		}
	},

	addChild : function(sceneNode){
		this.children[sceneNode.name] = sceneNode;
		sceneNode.parent = this;
		this._childrenNum++;
	},

	createChild : function(name){
		var sceneNode = new qtre.sceneNode(name);
		sceneNode.parent = this;
		this.addSceneNode(sceneNode);
		this._childrenNum++;
	},

	removeChild : function(name){
		this._hasChild(name);
		this.children[name] = null;
		this._childrenNum--;
	},
	
	getChild : function(name){
		this._hasChild(name);
		return this.children[name];
	},

	getChildrenNum : function(){
		return this._childrenNum;
	},

	getParent : function(){
		return this.parent;
	},

	attachObject : function(obj, name){
		this._objects[name] = obj;
		obj.owner = this;
	},

	remove : function(){
		delete this;
	},

	identity : function(){
		tdl.fast.identity4(this.worldMatrix);
	},

	translate : function(v){
		tdl.fast.matrix4.translate(this.worldMatrix, v);
	},

	rotateX : function(angle){
		var q = tdl.quaternions.rotationX(angle);
		tdl.fast.matrix4.mul(this.worldMatrix, tdl.quaternions.quaternionToRotation(q), this.worldMatrix);
	},

	rotateY : function(angle){
		var q = tdl.quaternions.rotationY(angle);
		tdl.fast.matrix4.mul(this.worldMatrix, tdl.quaternions.quaternionToRotation(q), this.worldMatrix);
	},

	rotateZ : function(angle){
		var q = tdl.quaternions.rotationZ(angle);
		tdl.fast.matrix4.mul(this.worldMatrix, tdl.quaternions.quaternionToRotation(q), this.worldMatrix);
	},

	rotate : function(axis, angle){
		var q = tdl.quaternions.axisRotation(axis, angle);
		tdl.fast.matrix4.mul(this.worldMatrix, tdl.quaternions.quaternionToRotation(q), this.worldMatrix);
	},

	scale : function(v){
		tdl.fast.matrix4.scale(this.worldMatrix, v);
	},

	setPosition : function(v){
		this.worldMatrix = tdl.fast.translation(this.worldMatrix, v);
	},

	getPosition : function(){
		
	},

	getWorldPosition : function(){
		
	},
	
	_hasChild : function(name){
		if(qtre.core.isset(this.children[name])){
			throw("scene node " + this.name + " can't find child: " + name)
		}
	}
}
	